package com.sojson.util.id;

import com.sojson.util.encrypt.EncryptUtil;
import com.sojson.util.id.impl.SnowflakeIdUtilImpl;
import com.sojson.util.id.impl.UuidUtilImpl;

/**
 * ID工具类
 *
 * @author lyh
 * 2020/11/26 15:59
 */
public class IdUtil {

    /** 编号头 */
    private static final String HEAD = "1";
    private static final UuidUtilImpl UUID = new UuidUtilImpl();
    private static final SnowflakeIdUtilImpl SNOWFLAKE_ID = new SnowflakeIdUtilImpl();

    /**
     * 生成UUID
     *
     * @return
     */
    public static String uuid() {
        return UUID.generate();
    }

    /**
     * 生成雪花ID
     *
     * @return
     */
    public static long snowflakeId() {
        return SNOWFLAKE_ID.generate();
    }

    /**
     * 生成订单ID
     *
     * @return
     */
    public static String orderNo() {
        return orderNo(HEAD);
    }

    /**
     * 生成订单ID
     *
     * @return
     */
    public static String orderNo(String key) {
        return String.valueOf(SNOWFLAKE_ID.generate());
    }

    /**
     * 协议编号,格式XY20201126161000ABCD
     */
    public static String mean() {
        return orderNo("XY");
    }

    /**
     * 邀请码编号,格式ID+YQM20201126161000ABCD
     */
    public static String yqm(String id) {
        return orderNo(id + "YQM");
    }

    /**
     * 助力码,格式活动编号,当前用户编号
     */
    public static String helpCodeNo(String activityNo, String userId) {
        return encryptionReversible(activityNo + "," + userId);
    }

    /**
     * 解密助力码,格式活动ID,当前用户ID
     */
    public static String parseHelpCodeNo(String helpCode) {
        return decryption(helpCode);
    }

    /**
     * 可逆的加密方法
     *
     * @param inStr
     * @return
     */
    private static String encryptionReversible(String inStr) {
        return EncryptUtil.encryptReverse(inStr);
    }

    /**
     * 解密方法
     *
     * @param inStr
     * @return
     */
    private static String decryption(String inStr) {
        return EncryptUtil.decrypt(inStr);
    }

}